import { parse } from 'querystring';

/* eslint no-useless-escape:0 import/prefer-default-export:0 */
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

export const isUrl = (path: string): boolean => reg.test(path);

export const isAntDesignPro = (): boolean => {
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
    return true;
  }
  return window.location.hostname === 'preview.pro.ant.design';
};

// 给官方演示站点用，用于关闭真实开发环境不需要使用的特性
export const isAntDesignProOrDev = (): boolean => {
  const { NODE_ENV } = process.env;
  if (NODE_ENV === 'development') {
    return true;
  }
  return isAntDesignPro();
};

export const getPageQuery = () => parse(window.location.href.split('?')[1]);


/**
 * 生成表格查询参数,后台统一处理请求参数
 * @param filters 
 */
export const getQuertParam = (filters: any) => {
  let param = {};
  if (filters && Object.keys(filters).length > 0) {
    Object.keys(filters).forEach((item) => {
      // 过滤分页信息
      if (item == 'current' || item == 'pageSize') return;
      if (filters[item] && filters[item].length > 0) {
        param[item] = filters[item];
      }
    });
  }
  return param;
};

/**
 * 导出excel专用
 * @param url 
 */
export const exportExcel=(url:string)=>{
  // 创建a链接,添加后删除
  const aLink = document.createElement('a');
  aLink.style.display = 'none';

  // 解决param参数大括号问题，要转义
  let deurl = encodeURI(url);

  aLink.href = deurl;
  document.body.appendChild(aLink);
  aLink.click();
  document.body.removeChild(aLink);
}

